'''
Data classes for tilesets
'''

from PyQt4 import QtCore
from rgeSave import *
import os
import rgeViews
from rgeSystem import *
import rgeExtra
import rgeBase

#standard keys
TS_IMAGE_KEY = "_tileset_image"
TS_WIDTH_KEY = "_tileset_width"
TS_HEIGHT_KEY = "_tileset_height"
TS_DEFAULT_KEY = "_tileset_default_tile"

#standard indexes
#strings
TS_IMAGE_INDEX = 0
#ints
TS_WIDTH_INDEX = 0
TS_HEIGHT_INDEX = 1
TS_DEFAULT_INDEX = 2

class TilesetData(rgeBase.BaseData):
    def __init__(self, name = "No_Name"):
        super(TilesetData, self).__init__(name)

        self.setFilename(TILESET_DIR + '/' + name + TILESET_EXTENSION)

        #data (saved)
        self.addStandard(rgeExtra.StringItem("", TS_IMAGE_KEY, TS_IMAGE_INDEX))
        self.addStandard(rgeExtra.IntItem(1, TS_WIDTH_KEY, TS_WIDTH_INDEX))
        self.addStandard(rgeExtra.IntItem(1, TS_HEIGHT_KEY, TS_HEIGHT_INDEX))

        #editor only stuff (saved)
        self.addStandard(rgeExtra.IntItem(0, TS_DEFAULT_KEY, TS_DEFAULT_INDEX))

        #editor only stuff, not saved
        self.tileNumberX = 1
        self.tileNumberY = 1
        self.currentFilepath = QtCore.QString("") #working filepath

    def getImageFilepath(self):
        return self.getStandard(TS_IMAGE_KEY).getValue()

    def getWidth(self):
        return self.getStandard(TS_WIDTH_KEY).getValue()

    def getHeight(self):
        return self.getStandard(TS_HEIGHT_KEY).getValue()

    def getDefaultTile(self):
        return self.getStandard(TS_DEFAULT_KEY).getValue()

    def getTileNumX(self):
        return self.tileNumberX

    def getTileNumY(self):
        return self.tileNumberY

    def setImageFilepath(self, Filepath):
        self.getStandard(TS_IMAGE_KEY).setValue(Filepath)

    def setDefaultTile(self, NewNum):
        self.getStandard(TS_DEFAULT_KEY).setValue(NewNum)
        self.edited()

    def setWidth(self, NewValue):
        self.getStandard(TS_WIDTH_KEY).setValue(NewValue)
        self.setTileNumber()
        self.edited()

    def setHeight(self, NewValue):
        self.getStandard(TS_HEIGHT_KEY).setValue(NewValue)
        self.setTileNumber()
        self.edited()

    def setTileNumberX(self, NewValue):
        from rgeSystem import mainWindow
        size = mainWindow.glwidget.getImageSize(self.currentFilepath)
        self.setWidth(int(size.width() / NewValue))
        self.edited()

    def setTileNumberY(self, NewValue):
        from rgeSystem import mainWindow
        size = mainWindow.glwidget.getImageSize(self.currentFilepath)
        self.setHeight(int(size.height() / NewValue))
        self.edited()

    def setTileNumber(self):
        from rgeSystem import mainWindow
        size = mainWindow.glwidget.getImageSize(self.currentFilepath)
        self.tileNumberX = int(size.width() / self.getWidth())
        self.tileNumberY = int(size.height() / self.getHeight())
        if self.tileNumberX < 1:
            self.tileNumberX = 1
        if self.tileNumberY < 1:
            self.tileNumberY = 1


    def setCurrentFilepath(self, NewLoc, ChangeSize = True):
        from rgeSystem import mainWindow
        if mainWindow.glwidget.checkImageValid(NewLoc):
            self.currentFilepath = NewLoc
            if ChangeSize:
                size = mainWindow.glwidget.getImageSize(self.currentFilepath)
                self.setWidth(size.width())
                self.setHeight(size.height())
            self.setTileNumber()
            self.edited()
        else:
            print "Bad new filepath: " + NewLoc

    def getCurrentFilepath(self):
        return self.currentFilepath

    def moveImage(self):
        GameFilepath = rgeViews.getGameFilepath()
        if self.getCurrentFilepath() == GameFilepath + self.getImageFilepath(): #skip if already there
            return
        oldImage = None
        try:
            oldImage = open(self.currentFilepath, "rb")
        except Exception as e:
            print "failure opening old image, aborting"
            return
        newImageLoc = GameFilepath + self.getFilename()[:-len(TILESET_EXTENSION)] + IMAGE_EXTENSIONS
        checkPathExists(newImageLoc[:-len(os.path.basename(str(newImageLoc)))])
        newImage = open(newImageLoc, "wb")
        newImage.write(oldImage.read())
        newImage.close()
        self.setCurrentFilepath(newImageLoc)
        self.setImageFilepath(self.currentFilepath[len(GameFilepath):])

    def saveData(self):
        self.moveImage()
        super(TilesetData, self).saveData()

    def loadFromFile(self, Filename, Unchanged = False):
        super(TilesetData,self).loadFromFile(Filename, Unchanged)
        self.setCurrentFilepath(rgeViews.getGameFilepath() + self.getImageFilepath(), False)

